package com.wonderful.plugin2;

import com.android.build.gradle.AppExtension;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ModuleVersionIdentifier;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Desc: DependenciesPlugin
 * @Author: wonderful
 * @Time: 2023/10/31 20:39
 */
public class DependenciesPlugin implements Plugin<Project> {

    private final String TAG = "WonderfulPlugin >>> ";

    @Override
    public void apply(Project project) {
        System.out.println(TAG + this.getClass().getName());

        DependenciesPluginExtension extension = project.getExtensions()
                .create("printDependencies", DependenciesPluginExtension.class);

        // 扩展的配置要在 project.afterEvaluate 之后获取
        // 因为配置阶段完成，才能读取参数，且配置完成，才能拿到所有的依赖
        project.afterEvaluate(pro -> {
            // 默认开启打印
            extension.getEnable().convention(false);
            if (extension.getEnable().get()) {
                // debug/release也可以加配置
                print("has open print dependencies");
                AppExtension appExtension = project.getExtensions().getByType(AppExtension.class);

                appExtension.getApplicationVariants().all(applicationVariant -> {
                    print("applicationVariant.getName() = " + applicationVariant.getName());
                    // 方式一：build.gradle 文件中添加的依赖
                    Configuration configuration = project.getConfigurations()
                            .getByName(applicationVariant.getName() + "CompileClasspath");
                    Set<Dependency> allDependencies = configuration.getAllDependencies();
                    //for (Dependency dependency : allDependencies) {
                    //    print("dependency === " + dependency.getGroup() +
                    //    ":" + dependency.getName() + ":" + dependency.getVersion());
                    //}

                    List<String> androidLibs = new ArrayList<>();
                    List<String> otherLibs = new ArrayList<>();

                    // 方式二：所有的依赖，包括依赖中的依赖
                    configuration.getResolvedConfiguration()
                            .getLenientConfiguration()
                            .getAllModuleDependencies()
                            .forEach(resolvedDependency -> {
                                ModuleVersionIdentifier identifier = resolvedDependency.getModule().getId();
                                if (identifier.getGroup().contains("androidx")
                                        || identifier.getGroup().contains("com.google")
                                        || identifier.getGroup().contains("org.jetbrains")) {
                                    androidLibs.add(identifier.getGroup() + ":" + identifier.getName() + ":" + identifier.getVersion());
                                } else {
                                    otherLibs.add(identifier.getGroup() + ":" + identifier.getName() + ":" + identifier.getVersion());
                                }
                            });

                    // 官方库
                    print("----------Official library start----------");
                    androidLibs.forEach(System.out::println);
                    print("----------Official library end----------");

                    // 三方库
                    print("----------Third-party library start----------");
                    otherLibs.forEach(System.out::println);
                    print("----------Third-party library end----------");
                });

            } else {
                print("has close print dependencies");
            }
        });
    }

    private void print(String msg) {
        System.out.println(TAG + msg);
    }
}
